//
//  BianjishouhuoViewController.m
//  tianjin
//
//  Created by 鞠超 on 16/8/11.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "BianjishouhuoViewController.h"

@interface BianjishouhuoViewController () <UIScrollViewDelegate, UITextFieldDelegate>

@property (nonatomic, strong) UIScrollView * scrollView;

//
@property (nonatomic, strong) UITextField * text_shoujianren;
@property (nonatomic, strong) UITextField * text_tel;
@property (nonatomic, strong) UITextField * text_address;

@end

@implementation BianjishouhuoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void)p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    _topView.backgroundColor = [UIColor blackColor];
    
    _lblTitle.text = @"编辑收货地址";
    _lblTitle.textColor = [UIColor whiteColor];
    _lblTitle.font = [UIFont systemFontOfSize:19];
    
    UIImageView * image_return = [[UIImageView alloc] initWithFrame:CGRectMake(15, 32, 10, 20)];
    image_return.image = [UIImage imageNamed:@"zuofanhui@2x.png"];
    [_topView addSubview:image_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 20, 35, 44);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    self.scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.scrollView.contentSize = CGSizeMake(0, SCREEN_HEIGHT - 60);
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    
    
    //收件人
    UIView * view_1 = [[UIView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, 55)];
    view_1.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:view_1];
    
    UIView * view_line1 = [[UIView alloc] initWithFrame:CGRectMake(0, 54, SCREEN_WIDTH, 1)];
    view_line1.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [view_1 addSubview:view_line1];
    
    UILabel * lbl_1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 70, 15)];
    lbl_1.text = @"收件人 :";
    lbl_1.font = [UIFont systemFontOfSize:15];
    [view_1 addSubview:lbl_1];
    
    self.text_shoujianren = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(lbl_1.frame) + 10, 17.5, SCREEN_WIDTH - CGRectGetMaxX(lbl_1.frame) - 20, 20)];
    self.text_shoujianren.delegate = self;
    self.text_shoujianren.text = @"上课的";
    self.text_shoujianren.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.text_shoujianren.font = [UIFont systemFontOfSize:15];
    self.text_shoujianren.returnKeyType = UIReturnKeyDone;
    [view_1 addSubview:self.text_shoujianren];
    
    
    //手机
    UIView * view_2 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(view_1.frame), SCREEN_WIDTH, 55)];
    view_2.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:view_2];
    
    UIView * view_line2 = [[UIView alloc] initWithFrame:CGRectMake(0, 54, SCREEN_WIDTH, 1)];
    view_line2.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [view_2 addSubview:view_line2];
    
    UILabel * lbl_2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 70, 15)];
    lbl_2.text = @"手机号码 :";
    lbl_2.font = [UIFont systemFontOfSize:15];
    [view_2 addSubview:lbl_2];
    
    self.text_tel = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(lbl_2.frame) + 10, 17.5, SCREEN_WIDTH - CGRectGetMaxX(lbl_2.frame) - 20, 20)];
    self.text_tel.delegate = self;
    self.text_tel.text = @"123456789123";
    self.text_tel.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.text_tel.font = [UIFont systemFontOfSize:15];
    self.text_tel.returnKeyType = UIReturnKeyDone;
    [view_2 addSubview:self.text_tel];
    
    
    //详细地址
    UIView * view_3 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(view_2.frame), SCREEN_WIDTH, 55)];
    view_3.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:view_3];
    
    UIView * view_line3 = [[UIView alloc] initWithFrame:CGRectMake(0, 54, SCREEN_WIDTH, 1)];
    view_line3.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [view_3 addSubview:view_line3];
    
    UILabel * lbl_3 = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 70, 15)];
    lbl_3.text = @"详细地址 :";
    lbl_3.font = [UIFont systemFontOfSize:15];
    [view_3 addSubview:lbl_3];
    
    self.text_address = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(lbl_3.frame) + 10, 17.5, SCREEN_WIDTH - CGRectGetMaxX(lbl_3.frame) - 20, 20)];
    self.text_address.delegate = self;
    self.text_address.text = @"详细地址。。。。。。。";
    self.text_address.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.text_address.font = [UIFont systemFontOfSize:15];
    self.text_address.returnKeyType = UIReturnKeyDone;
    [view_3 addSubview:self.text_address];
    
    
    //
    UIButton * btn_baocun = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_baocun.frame = CGRectMake(15, CGRectGetMaxY(view_3.frame) + 20, SCREEN_WIDTH - 30, 44);
    btn_baocun.layer.cornerRadius = 5;
    btn_baocun.backgroundColor = navi_bar_bg_color;
    [btn_baocun setTitle:@"保存修改" forState:(UIControlStateNormal)];
    [btn_baocun setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    btn_baocun.titleLabel.font = [UIFont systemFontOfSize:17];
    [self.scrollView addSubview:btn_baocun];
    [btn_baocun addTarget:self action:@selector(btn_baocunAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

#pragma mark - 点击保存
- (void )btn_baocunAction:(UIButton *)sender
{
    [self.text_shoujianren resignFirstResponder];
    [self.text_tel resignFirstResponder];
    [self.text_address resignFirstResponder];
    
    [UIView animateWithDuration:0.7 animations:^{
        
        self.scrollView.contentOffset = CGPointMake(0, 0);
        
    } completion:^(BOOL finished) {
        
    }];
    
    NSLog(@"点击保存");
}


#pragma mark - textfiled代理
- (BOOL )textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    [UIView animateWithDuration:0.7 animations:^{
        
        self.scrollView.contentOffset = CGPointMake(0, 0);
        
    } completion:^(BOOL finished) {
        
    }];
    
    return YES;
}

- (BOOL )textFieldShouldBeginEditing:(UITextField *)textField
{
    if(SCREEN_WIDTH == 320)
    {
        if([textField isEqual:self.text_tel])
        {
            [UIView animateWithDuration:0.7 animations:^{
                
                self.scrollView.contentOffset = CGPointMake(0, 15);
                
            } completion:^(BOOL finished) {
                
            }];
        }
        else if([textField isEqual:self.text_address])
        {
            [UIView animateWithDuration:0.7 animations:^{
                
                self.scrollView.contentOffset = CGPointMake(0, 75);
                
            } completion:^(BOOL finished) {
                
            }];
        }
    }
    
    return YES;
}

#pragma mark - scrollView代理
- (void )scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.text_shoujianren resignFirstResponder];
    [self.text_tel resignFirstResponder];
    [self.text_address resignFirstResponder];
}
















@end
